package common.config.feign;

import common.util.Constants;
import common.util.HttpRequestUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 米泽鹏
 * @since 2022-3-23 下午 5:46
 */
@Component
@Slf4j
public class FeignInterceptor implements RequestInterceptor {

	@Override
	public void apply(RequestTemplate template) {
		// 使feign调用携带token
		HttpServletRequest httpServletRequest = HttpRequestUtil.getRequest();
		if (httpServletRequest != null) {
			String authorization = httpServletRequest.getHeader(Constants.AUTHORIZATION_HEADER);
			template.header(Constants.AUTHORIZATION_HEADER, authorization);
		}
	}

}
